#!/bin/bash

if [ $# -eq 0 ];
then
    echo "Must give an action."
    echo "Build Golang routine."
    echo "/> ./dkr.sh gobuild <object_routine> <entry_source_file>"
    echo "Build Docker image."
    echo "/> ./dkr.sh build <image> <version>"
    echo "Clean container and image."
    echo "/> ./dkr.sh clean <container_name>" 
    exit
fi

if [[ $1 = 'clean' ]];
then
    container_image=`docker ps | grep $2 | awk '{print $2}'`
    container_id=`docker ps | grep $2 | awk '{print $1}'`
    #echo "IMAGE -> $container_image"
    #echo "ID -> $container_id" 
    #container_status=``
    docker stop $container_id
    echo "Container $2 stoped."
    docker rm $2
    echo "Container $2 removed."
    docker rmi $container_image
    echo "Image -> $container_image deleted."
elif [[ $1 = 'build' ]];
then
    echo "Build image for $2:$3"
    docker build --rm -t registry.apps.k8s.supor.cn/jinquan/$2:$3 -f Dockerfile .
elif [[ $1 = 'gobuild' ]];
then
    echo "Build golang binary routine."
    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -ldflags '-extldflags "-static"' -o $2 $3
fi

